package activity.interceptor;

import org.activiti.engine.impl.agenda.AbstractOperation;
import org.activiti.engine.impl.interceptor.DebugCommandInvoker;
import org.activiti.engine.logging.LogMDC;

/**
 * @Author: 蔡翔
 * @Date: 2019/8/25 14:58
 * @Version 1.0
 */
public class MDCCommandInvoker extends DebugCommandInvoker {
    @Override
    public void executeOperation(Runnable runnable) {
        //执行之前 设置MDC为自动
        LogMDC.setMDCEnabled(true);

        if(runnable instanceof AbstractOperation){
            AbstractOperation abstractOperation = (AbstractOperation) runnable;
            if(abstractOperation.getExecution() !=null){
                //不为null 便是可以操作的流程对象，我们这里就需要把日志记录一下（把这个对象放入MDC上下文里面去）
                LogMDC.putMDCExecution(abstractOperation.getExecution());
            }
        }
        super.executeOperation(runnable);
        //为了保持环境的清洁需要把 MDC上下文信息给清除掉
        LogMDC.clear();
    }
}
